package com.guanglan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guanglan.entity.SysRole;
import com.guanglan.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 作者: 光蓝Comet
 * Date: 2018-06-20 15:29
 * Description: 一句话说明这个类的作用
 * Since 1.0
 */
@RequestMapping(value = "/system/role")
@Controller
public class SysRoleController {

    private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/index")
    public String roleIndex(@RequestParam(name = "roleName",defaultValue = "",required = false) String roleName,
                            @RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,
                            @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize,
                            Model model){
        logger.info("Current user into the user method!");
        PageHelper.startPage(nowPage, pageSize);
        PageHelper.orderBy("create_time desc");
        List<SysRole> roles = sysRoleService.selectSysRolesByRoleName(roleName);
        PageInfo<SysRole> pageInfo = new PageInfo<>(roles);
        model.addAttribute("pageInfo",pageInfo);
        return "role/roleIndex";
    }

    @RequestMapping(value = "/editPage")
    public String editPage(@RequestParam(value="editId",required=false) String editId,Model model){
        if (StringUtils.trimToNull(editId) != null){
            SysRole sysRole = sysRoleService.selectByPrimaryKey(Long.valueOf(editId));
            model.addAttribute("role",sysRole);
        }
        return "role/roleEdit";
    }

    @RequestMapping(value = "/editRole")
    @ResponseBody
    public String editRole(@RequestParam(value="editId",required=false) String editId,
                           @RequestParam(name = "roleName",defaultValue = "",required = false) String roleName){

        SysRole sysRole = new SysRole();
        sysRole.setRoleName(roleName);
        try {
            if (StringUtils.trimToNull(editId) != null){
                sysRole.setId(Long.valueOf(editId));
                sysRoleService.updateByPrimaryKeySelective(sysRole);
                return "{\"msg\":\"修改成功\"}";
            }else {
                sysRoleService.insertSelective(sysRole);
                return "{\"msg\":\"添加成功\"}";
            }
        }catch (NullPointerException e){
            e.printStackTrace();
            return "{\"msg\":\"系统错误\"}";
        }
    }

    @RequestMapping(value = "/delRole")
    @ResponseBody
    public String delRole(@RequestParam(value="delId",required=false) Long delId){
        sysRoleService.deleteByPrimaryKey(delId);
        return "{\"msg\":\"删除成功\"}";
    }

}
